package com.suwish.pc.startup;

import java.io.File;

import javax.swing.UIManager;

import com.suwish.device.util.Platform;
import com.suwish.pc.util.StringUtils;

/**
 * 
 * 
 * 打包的时候需要将此类单独打为jar作为启动项。</p>
 * 
 * 一并打入启动项JAR的资源有<br/>
 * 
 * {@link Platform} {@link UIClassLoader}
 * </p>
 * 貌似把{@link Platform}加进来不合适....
 * @author Min
 *
 */
public class Launcher {
	
	public Launcher() throws Exception{
		initEvt();
	}
	
	private void initEvt() throws Exception{
					
		String value = Platform.getAppDir();
			
		String libPath = System.getProperty("java.library.path");
        String classPath = System.getProperty("java.class.path");
//		System.err.println(classPath);
//		System.err.println(libPath);
		String[] classPaths = Platform.getClassPath().split(Platform.getClassPathSeparator());
		for(String path : classPaths){
			if(path.endsWith("build")){				
				System.setProperty(Platform.PHONE_BRIDGE_LIB_DIR, path + File.separator + "lib");
				System.setProperty(Platform.PHONE_BRIDGE_RESOURCES_DIR, path + File.separator + "resources");
				break;
			}
		}
		if(StringUtils.isEmpty(Platform.getAppLibDir())){
			System.setProperty(Platform.PHONE_BRIDGE_LIB_DIR, value + File.separator + "lib");
		}
		if(StringUtils.isEmpty(Platform.getAppResourcesDir())){
			System.setProperty(Platform.PHONE_BRIDGE_RESOURCES_DIR, value + File.separator + "resources");
		}
		StringBuilder pathBuilder = new StringBuilder();
		pathBuilder.append(classPath + Platform.getClassPathSeparator() + value + "/");
		pathBuilder.append(Platform.getClassPathSeparator() + libPath);
		System.setProperty("java.class.path", pathBuilder.toString());
//		System.out.println("classPath " + Platform.getClassPath());
		System.setProperty("java.library.path", pathBuilder.toString());
//		if(StringUtils.isEmpty(Platform.getAppResourcesDir())){
//			System.setProperty(Platform.PHONE_BRIDGE_RESOURCES_DIR, value + File.separator + "resources");
//		}
//		System.out.println(AdbManager.ADB_PATH);
		
				
	}
	
	private void run() throws Exception{
//		Class<?> setting = getClass().getClassLoader().loadClass("com.suwish.pc.Setting");
//		setting.getMethod("initConfiguration", (Class[])null).invoke(setting, (Object[])null);
		ClassLoader loader = new UIClassLoader(findParentClassLoader(), new File(Platform.getAppLibDir()));
		Thread.currentThread().setContextClassLoader(loader);	
		Class<?> clazz = loader.loadClass("com.suwish.pc.Startup");
		Object o = clazz.newInstance();
		clazz.getMethod("startup", (Class[])null).invoke(o, (Object[])null);
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		Launcher lanucher =  new Launcher();
//		lanucher.initLookAndFeel();
		lanucher.run();
	}
	private ClassLoader findParentClassLoader() {
        ClassLoader parent = Thread.currentThread().getContextClassLoader();
        if (parent == null) {
            parent = getClass().getClassLoader();
            if (parent == null) {
                parent = ClassLoader.getSystemClassLoader();
            }
        }
        return parent;
    }
	/**
	 * @deprecated
	 */
	private void initLookAndFeel(){
		try{
			if(!Platform.isWindows())
			UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
			else
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}
